package com.fh.util;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

/**
 * 网易云信
 * @author Administrator
 *
 */
public class NeteaseSms {
	
	/**
	 * 发送验证码短信
	 * @param mobile
	 * @param code
	 * @param templateId
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	public static String sendCode(String mobile,String code,String templateId) throws UnsupportedEncodingException{
		String url = "https://api.netease.im/sms/sendcode.action";
		// 设置请求的的参数
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		nvps.add(new BasicNameValuePair("params", "["+code+"]"));
		if(templateId != null){
			nvps.add(new BasicNameValuePair("templateid", templateId));
		}
		// 参数 jsonArray形式
		nvps.add(new BasicNameValuePair("mobile", mobile));
		System.out.println(nvps);
		return post(url, nvps);
	}
	/**
	 * 使用默认短信模板
	 * @param mobile
	 * @param code
	 * @return
	 */
	public static void sendCode(String mobile,String code){
		NeteaseSms sms =new NeteaseSms();
		try {
			sms.sendCode(mobile, code,null);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	/**
	 * 发送模板短信
	 * @param templateId
	 * @param mobiles ['手机号码','手机号码']
	 * @param params "['参数内容','参数内容','参数内容',....]"
	 * @param 请注意参数内容单引号
	 */
	public static String sendTemplateSms(String templateId,String mobiles,String params){
		DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/sms/sendtemplate.action";
        //设置请求的的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        //模板id			
		nvps.add(new BasicNameValuePair("templateid", templateId));
		//参数jsonArray形式
		nvps.add(new BasicNameValuePair("mobiles","["+mobiles+"]"));
		nvps.add(new BasicNameValuePair("params", "["+params+"]"));
		return post(url, nvps);
	}
	
	
	/**
	 * 发送模板短信
	 * @param mobiles 
	 * @param params 
	 * @param 请注意参数内容单引号
	 */
	public static String sendTemplateSms(String mobiles,String params){
		DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/sms/sendtemplate.action";
        //设置请求的的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        //模板id			
		nvps.add(new BasicNameValuePair("templateid", "3054288"));
		//参数jsonArray形式
		nvps.add(new BasicNameValuePair("mobiles","["+mobiles+"]"));
		nvps.add(new BasicNameValuePair("params", "["+params+"]"));
		return post(url, nvps);
	}
	
	/**
	 * 发送模板短信
	 * @param mobiles 
	 * @param params 
	 * @param 请注意参数内容单引号
	 */
	public static String sendTemplateSm(String mobiles,String params){
		DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/sms/sendtemplate.action";
        //设置请求的的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        //模板id			
		nvps.add(new BasicNameValuePair("templateid", "3063396"));
		//参数jsonArray形式
		nvps.add(new BasicNameValuePair("mobiles","["+mobiles+"]"));
		nvps.add(new BasicNameValuePair("params", "["+params+"]"));
		return post(url, nvps);
	}
	
	/**
	 * 发送模板短信
	 * @param mobiles 
	 * @param params 
	 * @param 请注意参数内容单引号
	 */
	public static String sendTemplateS(String mobiles,String params){
		DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/sms/sendtemplate.action";
        //设置请求的的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        //模板id			
		nvps.add(new BasicNameValuePair("templateid", "3050475"));
		//参数jsonArray形式
		nvps.add(new BasicNameValuePair("mobiles","["+mobiles+"]"));
		nvps.add(new BasicNameValuePair("params", "["+params+"]"));
		return post(url, nvps);
	}
	
	/**
	 * 发送模板短信
	 * @param mobiles 
	 * @param params 
	 * @param 请注意参数内容单引号
	 */
	public static String sendTemplateSmss(String mobiles,String params,String param){
		DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/sms/sendtemplate.action";
        //设置请求的的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        //模板id			
		nvps.add(new BasicNameValuePair("templateid", "3057541"));
		//参数jsonArray形式
		nvps.add(new BasicNameValuePair("mobiles","["+mobiles+"]"));
		nvps.add(new BasicNameValuePair("params", "["+params+","+param+"]"));
		return post(url, nvps);
	}
	
	/**
	 * 发送模板短信
	 * @param mobiles 
	 * @param params 
	 * @param 请注意参数内容单引号
	 */
	public static String sendTemplateSmsss(String mobiles,String params,String param){
		DefaultHttpClient httpClient = new DefaultHttpClient();
        String url = "https://api.netease.im/sms/sendtemplate.action";
        //设置请求的的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        //模板id			
		nvps.add(new BasicNameValuePair("templateid", "3054483"));
		//参数jsonArray形式
		nvps.add(new BasicNameValuePair("mobiles","["+mobiles+"]"));
		nvps.add(new BasicNameValuePair("params", "["+params+","+param+"]"));
		return post(url, nvps);
	}
	

	public static void main(String[] args) {
			NeteaseSms.sendTemplateSmsss("15613099294","15613099294","123");
	}
	
	/**
	 * 校验验证码
	 */
	public static String  verifyCode(String mobile,String code){
		String url = "https://api.netease.im/sms/verifycode.action";
		// 设置请求的的参数
		List<NameValuePair> nvps = new ArrayList<NameValuePair>();
		//参数 jsonArray形式
		nvps.add(new BasicNameValuePair("mobile", mobile));
		//code 参数
		nvps.add(new BasicNameValuePair("code", code));
		return post(url, nvps);
	}
	/**
	 * post 短信的公共部分
	 * @param url
	 * @param nvps
	 * @return
	 */
	public static String  post(String url,List<NameValuePair> nvps){
		DefaultHttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(url);
		String appKey ="620db9a9c6a2cc33ad58168265de8bd2";
        String appSecret = "4cc79bfac48a";
        String nonce =  "123456";//nonce随机数
        String curTime = String.valueOf((new Date()).getTime() / 1000L);//time
        String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce ,curTime);//参考计算CheckSum的java代码
        // 设置请求的header
        httpPost.addHeader("AppKey", appKey);
        httpPost.addHeader("Nonce", nonce);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        
        try {
			httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        HttpResponse response = null;
		try {
			response = httpClient.execute(httpPost);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		String result = null;
        // 打印执行结果
        try {
        	result =  EntityUtils.toString(response.getEntity(), "utf-8");
		} catch (ParseException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return result;
	}

	public static void batchSms(final NeteaseObj sms){
		new Thread(new Runnable() {
			@Override
			public void run() {
				for (int i = 0; i < sms.getMobile().length; i++) {
					sendTemplateSms(sms.getTemplateId(), sms.getMobile()[i], (sms.getParams()[i]).toString());
				}
			}
		}).start();
	}

	
}
